package com.yonyou.ucf.mdf.app.isvdxq.other.service;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import com.yonyou.ucf.mdf.app.isvdxq.common.ApiYon;
import com.yonyou.ucf.mdf.app.isvdxq.other.mapper.impl.TranstypeMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.yonyisv.model.EventContent;
import com.yonyou.ucf.mdf.app.isvdxq.other.entity.TranstypeEntity;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.AccessTokenResponse;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.ContentBean;
import com.yonyou.ucf.mdf.app.isvdxq.utils.GenericResponse;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TranstypeService {
    @Autowired
    private BaseService baseService;
    @Autowired
    private TranstypeMapperImpl transtypeMapper;

    public TranstypeEntity SelectById(Long Id)
    {
        return transtypeMapper.selectById(Id);
    }
    public Boolean CheckEntity(Long Id)
    {
        int rInt = transtypeMapper.selectCount(Id);
        if (rInt > 0) return true;
        else return false;
    }
    public Integer InsertEntity(TranstypeEntity entity)
    {
        return transtypeMapper.insert(entity);
    }
    public Integer UpdateEntity(TranstypeEntity entity)
    {
        return transtypeMapper.updateByPrimaryKey(entity);
    }
    public List<TranstypeEntity> SelectAll()
    {
        return transtypeMapper.selectAll();
    }
    public Integer DeleteById(Long Id){
        return transtypeMapper.deleteByPrimaryKey(Id);
    }

    public int EventDel(EventContent content) throws IOException {
        ContentBean obj = RequestTool.getBean(content.getContent(), ContentBean.class);
        Long Id = Long.parseLong(obj.getId());
        Boolean flag = CheckEntity(Id);
        if (flag) {
            int rInt = DeleteById(Id);
            return rInt;
        }
        return 0;
    }

    public int EventEdit(EventContent content) throws IOException  {
        TranstypeEntity transtypeEntity = RequestTool.getBean(content.getContent(), TranstypeEntity.class);
        System.out.println(JSON.toJSONString(transtypeEntity));
        Boolean flag = CheckEntity(transtypeEntity.getId());
        if (flag) {
            int rInt = UpdateEntity(transtypeEntity);
            return rInt;
        } else {
            int rInt = InsertEntity(transtypeEntity);
            return rInt;
        }
    }

    /**
     * 主动获取交易类型列表
     * @param tenantId
     * @return
     * @throws IOException
     */
    public int EventItemEdit(String tenantId, String detailId) throws IOException {
        String gatewayUrl = baseService.buildBusinessUrl(tenantId);
        if (StringUtils.isEmpty(gatewayUrl)) return 0;

        AccessTokenResponse MyToken = baseService.getAccessToken3(tenantId);
        String token = MyToken.getAccessToken();
        String requrl = gatewayUrl + ApiYon.getTranstypeDetail + "?access_token=" + token;
        Map<String, String> params = new HashMap<>();
        // 除签名外的其他参数
        params.put("id", detailId);
        String resString = RequestTool.doGet(requrl, params);
        GenericResponse<TranstypeEntity> response =
                RequestTool.getBean(resString, new TypeReference<GenericResponse<TranstypeEntity>>() {
                });

        if (response.getCode().equals("200")) {
            TranstypeEntity transtypeEntity = response.getData();
            Boolean flag = CheckEntity(transtypeEntity.getId());
            if (flag) {
                int rInt = UpdateEntity(transtypeEntity);
                return rInt;
            } else {
                int rInt = InsertEntity(transtypeEntity);
                return rInt;
            }
        }

        return 0;
    }
}
